package controller.dictionary;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.ItemTypeServices;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.dictionary.ItemType;
import wt.entity.dictionary.ItemTypeExample;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */

@Controller

@RequestMapping("dictionary/itemType")
public class ItemTypeController extends BaseController {
    @Autowired
    ItemTypeServices itemTypeServices;

    @RequiresPermissions("dictionary:itemType:default")
    @RequestMapping("index")
    public String index() {
        return "dictionary/itemType/index";
    }

    @RequiresPermissions("dictionary:itemType:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            ItemType itemType = itemTypeServices.selectByPrimaryKey(id);
            if (itemType == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, itemType);
            map.put(StaticConstant.EDIT_URL, "dictionary/itemType/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "dictionary/itemType/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "dictionary/itemType/editDlg";
    }

    @RequiresPermissions("dictionary:itemType:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ItemType itemType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(itemType.getName(), new NotBlankFluentValidator("名称"))
                .on(itemType.getNum(), new NotBlankFluentValidator("编号"))
                .on(itemType.getSeries(), new NotNullFluentValidator("专业级别"))
                .on(itemType.getLive(), new NotNullFluentValidator("状态"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        ItemTypeExample itemTypeExample = new ItemTypeExample();
        itemTypeExample.createCriteria().andNameEqualTo(itemType.getName());
        itemTypeExample.or().andNumEqualTo(itemType.getNum());
        List<ItemType> itemTypeList = itemTypeServices.selectByExample(itemTypeExample);
        if (itemTypeList.isEmpty()) {
            itemTypeServices.insertSelectiveAutoInsertMajorType(itemType);
            return commonResult.setSuccess();
        } else {
            commonResult.setMessage("名称和编号都不能重复！");
            return commonResult.setError();
        }


    }

    @RequiresPermissions("dictionary:itemType:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ItemType itemType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(itemType.getName(), new NotBlankFluentValidator("名称"))
                .on(itemType.getNum(), new NotBlankFluentValidator("编号"))
                .on(itemType.getLive(), new NotNullFluentValidator("状态"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        ItemTypeExample itemTypeExample = new ItemTypeExample();
        itemTypeExample.createCriteria().andNameEqualTo(itemType.getName()).andIdNotEqualTo(itemType.getId());
        itemTypeExample.or().andNumEqualTo(itemType.getNum()).andIdNotEqualTo(itemType.getId());
        List<ItemType> itemTypeList = itemTypeServices.selectByExample(itemTypeExample);
        if (itemTypeList.isEmpty()) {
            commonResult.setStatus(itemTypeServices.updateByPrimaryKeySelective(itemType));
            return commonResult;
        } else {
            commonResult.setMessage("名称和编号都不能重复！");
            return commonResult.setError();
        }


    }

    @RequiresPermissions("dictionary:itemType:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        commonResult.setStatus(itemTypeServices.deleteByPrimaryKey(id));
        return commonResult;
    }

    @RequiresPermissions("dictionary:itemType:default")
    @ResponseBody
    @RequestMapping("select")
    public List<ItemType> select() throws Exception {

        ItemTypeExample itemTypeExample=new ItemTypeExample();
        itemTypeExample.setOrderByClause("id desc");
        return itemTypeServices.selectByExample(itemTypeExample);

    }


}
